﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace Common
{
    public class PageSafeHelper
    {
        private const string StrRegex = @"iframe|alert|confirm|prompt|script|exec|select|union|database|update|insert|from|truncate|drop|declare";

        public static bool PostData()
        {
            bool result = false;

            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form[i]))
                {
                    result = CheckData(HttpContext.Current.Request.Form[i]);
                    if (result)
                    {
                        break;
                    }
                }
            }
            return result;
        }

        public static bool GetData()
        {
            bool result = false;

            for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[i]))
                {
                    result = CheckData(HttpContext.Current.Request.QueryString[i]);
                    if (result)
                    {
                        break;
                    }
                }
            }
            return result;
        }

        public static bool CookieData()
        {
            bool result = false;
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                if (HttpContext.Current.Request.Cookies[i] != null
                    && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[i].Value))
                {
                    result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
                    if (result)
                    {
                        break;
                    }
                }
            }
            return result;

        }

        public static bool referer()
        {
            bool result = false;
            return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
        }

        public static bool CheckData(string inputData)
        {
            if (string.IsNullOrEmpty(inputData))
                return false;
            inputData = inputData.ToLower();
            if (Regex.IsMatch(inputData, StrRegex))
                return true;
            return false;
        }
    }
}
